package org.dynamo.database.ui.wizzard.helper;

/*******************************************************************************
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Jean Neveux - initial API and implementation
 *******************************************************************************/

import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.datatools.connectivity.IConnection;
import org.eclipse.datatools.connectivity.IConnectionFactory;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.IConnectionProfileProvider;
import org.eclipse.datatools.connectivity.ProfileManager;
import org.eclipse.datatools.connectivity.sqm.core.connection.ConnectionInfo;
import org.eclipse.datatools.modelbase.sql.schema.Database;

public class DatabaseHelper {
	private Map<Database, IConnectionProfile> mDataBases;
	private static DatabaseHelper instance = new DatabaseHelper();

	
	public static DatabaseHelper getInstace()
	{
		return instance;
	}
	
	/**
	 * Use the ProfileManager instance from the eclipse datatools connectivity
	 * plugin to retrieve all database connections
	 * 
	 * @return
	 */
	public Database[] getDbs() {
		Map<Database, IConnectionProfile> db = getDataBases();
		Set<Database> dbSet = db.keySet();
		return dbSet.toArray(new Database[dbSet.size()]);
	}

	public IConnectionProfile getConnectionProfile(Database db){
		return getDataBases().get(db);
	}
	
	private Map<Database, IConnectionProfile> getDataBases() {
		if (mDataBases == null) {
			mDataBases = new HashMap<Database, IConnectionProfile>();

			ProfileManager profileManager = ProfileManager.getInstance();
			IConnectionProfile[] profiles = profileManager.getProfiles();
			for (IConnectionProfile connectionProfile : profiles) {
				// use connectionInfo
				IConnection connection2 = connectionProfile
						.createConnection("org.eclipse.datatools.connectivity.sqm.core.connection.ConnectionInfo");
				ConnectionInfo conninfo = (ConnectionInfo) connection2
						.getRawConnection();
				if (conninfo.getSharedDatabase() != null) {
					Database db = conninfo.getSharedDatabase();
					String description = connectionProfile.getName();
					db.setDescription(description);
					mDataBases.put(db, connectionProfile);
				}
			}
		}
		return mDataBases;
	}

	
	public Connection getConnection(Database db) {
		IConnectionProfile ic = getConnectionProfile(db);
		IConnectionProfileProvider icp = ic.getProvider();
		
		for(Object fact : icp.getConnectionFactories().values())
		{
			IConnectionFactory cf = (IConnectionFactory)fact;
		    IConnection connection = cf.createConnection(ic);
		    
		    Object rawConn = connection.getRawConnection();
			if(rawConn instanceof Connection)
			{
				return (Connection)rawConn;
			}
		}
		return null;
	}
	
	public void connectAll()
	{
		for(IConnectionProfile obj : ProfileManager.getInstance().getProfiles())
		{
			IConnectionProfile ic = (IConnectionProfile)obj;
			ic.connect();
		}
	}
	
	/**
	 * Force a read of databases.
	 */
	public void resetDatabase()
	{
		mDataBases = null;
	}
}
